package com.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Orderinfo;
import com.common.Common;
import com.dao.OrderDao;
import com.utils.PageInfo;
import com.utils.PageUtil;
import com.utils.StringUtil;

/**
 * 订单维护
 * @author 14560
 *
 */
@WebServlet("/OrderServlet.do")
public class OrderServlet extends HttpServlet {
	private OrderDao _orderDao=new OrderDao();
	private DateTimeFormatter df=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss" );
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		String flag = request.getParameter("flag");
		if ("manage".equals(flag)) {
			manage(request, response);
		}else if ("shipments".equals(flag)) {
			shipments(request, response);
		}
		else if ("show".equals(flag)) {
			show(request, response);
		}
	}
	/*
	 * 订单管理
	 */
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String orderNo = request.getParameter("orderNo");
		if(!StringUtil.isEmpty(orderNo)) {
			orderNo = orderNo.trim();
		}
		String beginDate = request.getParameter("beginDate");
		if(!StringUtil.isEmpty(beginDate)) {
			beginDate = beginDate.trim();
		}
		String endDate = request.getParameter("endDate");
		if(!StringUtil.isEmpty(endDate)) {
			endDate = endDate.trim();
		}
		String orderState = request.getParameter("orderState");
		int pageSize = Common.PAGE_SIZE;
		int rowCount = _orderDao.getOrderCount(orderNo, beginDate, endDate, orderState);
		int pageIndex = 1;
		String pageIndexStr = request.getParameter("pageIndex");
		if (pageIndexStr != null) {
			pageIndex = Integer.parseInt(pageIndexStr);
		}
		PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
		List<Orderinfo> orderList = _orderDao.getOrderList(orderNo, beginDate, endDate, orderState,page);
		request.setAttribute("page", page);
		request.setAttribute("orderList", orderList);
		request.getRequestDispatcher("/order/order_manage.jsp").forward(request, response);

	}
	
	/*
	 * 订单发货
	 */
	private void shipments(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		LocalDateTime nowDateTime=LocalDateTime.now();
		int resut=_orderDao.shipments(idnumber,df.format(nowDateTime));
		if (resut == 1) {
			manage(request, response);
		}
	}
	
	/*
	 * 订单查看
	 */
	private void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		List<Map<String, Object>> ordergoods=_orderDao.getOrderGoods(idnumber);
		Orderinfo order=_orderDao.getOrder(idnumber);
		request.setAttribute("ordergoods", ordergoods);
		request.setAttribute("order", order);
		request.getRequestDispatcher("/order/order_show.jsp").forward(request, response);
	}
}
